//
//  HMVisitorView.swift
//  Weibo24
//
//  Created by apple on 17/3/27.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
import SnapKit

//  定义访客视图的代理协议
protocol HMVisitorViewDelegate: NSObjectProtocol {
    //  登录相关的操作
    func loginOperation()
}

//  自定义访客视图
class HMVisitorView: UIView {

    //  MARK: ---   懒加载控件
    //  旋转的视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //  罩层
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //  主页
    private lazy var homeImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //  访客视图信息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    //  注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //  手写代码拉伸
        //  resizableImage 表示图片拉伸的一个方法
//        image对象.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: UIImageResizingMode.stretch)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //  取消高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    //  登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //  手写代码拉伸
        //        UIImage().resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: UIImageResizingMode.stretch)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //  取消高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    //  MARK: ---定义代理对象的属性
    //  weak 代理对象弱引用
    weak var delegate: HMVisitorViewDelegate?
    //  MARK: ---定义闭包的属性
    var loginClosure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //  不支持xib
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor(white: 237.0 / 255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //  必须先添加控件再设置约束
        //  使用snapkit设置控件的布局
        cycleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
       
        homeImageView.snp.makeConstraints { (make) in
            make.center.equalTo(maskIconImageView)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(homeImageView)
            make.top.equalTo(cycleImageView.snp.bottom)
            make.width.equalTo(224)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
    }
    
    //  开启旋转动画
    private func startAnimation() {
        //  创建动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  目的地
        animation.toValue = 2 * M_PI
        //  指定动画时长
        animation.duration = 20
        //  动画的执行次数, MAXFLOAT最大值
        animation.repeatCount = MAXFLOAT
        //  动画失去焦点,动画会释放, 设置false在动画执行次数内不会释放动画对象
        animation.isRemovedOnCompletion = false
        //  执行动画,在layer执行
        cycleImageView.layer.add(animation, forKey: nil)
        
        
    }
    
    //  修改访客视图信息
    func updateVisitorInfo(imageName: String?, message: String?) {
        //  多个可选类型的判断
        if let imgName = imageName, let msg = message {
            //  表示不是首页, 是消息,发现,我的
            homeImageView.image = UIImage(named: imgName)
            messageLabel.text = msg
            //  隐藏旋转视图
            cycleImageView.isHidden = true
        } else {
            //  首页
            startAnimation()
        }
      
    }
    
    //  MARK: ---登录和注册按钮的点击事件
    @objc private func loginAction() {
        print("登录")
        //  使用代理对象调用代理方法
//        delegate?.loginOperation()
        //  执行闭包
        loginClosure?()
    }
    
    

}

extension HMVisitorView {
    private func test() {
        //  代码的方式完成自动布局
        //        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        //        //  需要掌握,因为如果大家以后封装一个框架是不应该依赖masnory, 一般封装框架的时候取用
        //        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
    }
}





